package org.ysu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.transaction.annotation.Transactional;
import org.ysu.entity.AdminClue;
import org.ysu.entity.AdminRole;
import org.ysu.entity.Clue;
import org.ysu.entity.Role;
import org.ysu.mapper.AdminClueMapper;
import org.ysu.mapper.AdminMapper;
import org.ysu.service.AdminClueService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 线索分配表 服务实现类
 * </p>
 *
 * @author CaoYuJia
 * @since 2024-08-21
 */
@Service
public class AdminClueServiceImpl extends ServiceImpl<AdminClueMapper, AdminClue> implements AdminClueService {

    @Resource
    AdminClueMapper adminClueMapper;

    @Override
//    事务：一组操作的集合，这些操作要么全部成功，要么全部失败
    @Transactional
    public boolean save(Long clueId, Long[] adminIds) {
        UpdateWrapper<AdminClue> wrapper = new UpdateWrapper();
        wrapper.eq("clue_id", clueId);
        this.remove(wrapper);
        List<AdminClue> list = new ArrayList<>();
        if (null != adminIds && adminIds.length != 0) {
            for (Long adminId : adminIds) {
                list.add(new AdminClue(adminId, clueId));
            }
        }
        return this.saveBatch(list);
    }


    @Override
    public List<AdminClue> getByClueId(Long clueId) {
        QueryWrapper<AdminClue> wrapper = new QueryWrapper<>();
        wrapper.eq("clue_id", clueId);
        return this.list(wrapper);
    }

    @Override
    public List<Clue> getClueListByAdminId(Long adminId) {
        return adminClueMapper.getClueListByAdminId(adminId);
    }

}
